package com.qdsg.ysg.doctor.ui.activity.prescription;

import android.view.View;
import android.widget.TextView;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.qdsg.ysg.doctor.R;
import com.qdsg.ysg.doctor.base.BaseActivity;
import com.qdsg.ysg.doctor.eventbus.PrescriptionAchieveEvent;
import com.qdsg.ysg.doctor.ui.adapter.PrescriptionStatusAdapter;
import com.qdsg.ysg.doctor.util.ToastUtil;
import com.rest.business.RestProxy;
import com.rest.response.BaseStringResponse;
import com.rest.response.PrescriptionStatusBean;
import com.rest.response.PrescriptionStatusDto;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;
import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;

/**
 * @Description: 开处方  处方状态审核页
 * @Author: lzp
 * @CreateDate: 2022/5/11 10:31
 * @UpdateRemark: 更新说明
 */
public class CheckPrescriptionStatusActivity extends BaseActivity {
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.tv_modify_prescription)
    TextView tvModifyPrescription;
    @BindView(R.id.tv_submit)
    TextView tvSubmit;
    @BindView(R.id.tv_achieve)
    TextView tvAchieve;
    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;


    String prescriptionId;
    PrescriptionStatusAdapter mAdapter;
    List<PrescriptionStatusBean> mList = new ArrayList<>();

    @Override
    protected int getLayoutResource() {
        return R.layout.activity_check_prescription_status;
    }

    @Override
    protected void initData() {
        prescriptionId = getIntent().getStringExtra("prescriptionId");
        queryPrescriptionStatus();
    }

    @Override
    protected void initViews() {
        tvTitle.setText("开处方");
        LinearLayoutManager manager = new LinearLayoutManager(this);
        manager.setOrientation(LinearLayoutManager.VERTICAL);
        recyclerView.setLayoutManager(manager);
        mAdapter = new PrescriptionStatusAdapter(mContext, mList);
        recyclerView.setAdapter(mAdapter);
    }

    /**
     * 处方审核状态
     */
    public void queryPrescriptionStatus() {
        showProgressDialog(mContext);
        RestProxy.getInstance().queryPrescriptionStatus(prescriptionId, new Observer<PrescriptionStatusDto>() {
            @Override
            public void onSubscribe(Disposable d) {

            }

            @Override
            public void onNext(PrescriptionStatusDto prescriptionStatusDto) {
                PrescriptionStatusBean statusBean1 = new PrescriptionStatusBean("库存核验");
                PrescriptionStatusBean statusBean2 = new PrescriptionStatusBean("合理用药审核");
                PrescriptionStatusBean statusBean3 = new PrescriptionStatusBean("创建处方");
                //status  1库存不足  2审核不过  3创建处方失败  4成功
                if (prescriptionStatusDto.data.status == 1) {
                    statusBean1.status = 2;
                    statusBean1.errorMsg = prescriptionStatusDto.data.errorMsg;
                } else if (prescriptionStatusDto.data.status == 2) {
                    statusBean1.status = 1;
                    statusBean2.status = 2;
                    statusBean2.errorMsg = prescriptionStatusDto.data.errorMsg;
                    statusBean2.level = prescriptionStatusDto.data.level;
                } else if (prescriptionStatusDto.data.status == 3) {
                    statusBean1.status = 1;
                    statusBean2.status = 1;
                    statusBean3.status = 2;
                    statusBean3.errorMsg = prescriptionStatusDto.data.errorMsg;
                } else if (prescriptionStatusDto.data.status == 4) {
                    statusBean1.status = 1;
                    statusBean2.status = 1;
                    statusBean3.status = 1;
                }
                mList.add(statusBean1);
                mList.add(statusBean2);
                mList.add(statusBean3);
                mAdapter.notifyDataSetChanged();

                if (prescriptionStatusDto.data.status == 4) {
                    tvAchieve.setVisibility(View.VISIBLE);
                    tvModifyPrescription.setVisibility(View.GONE);
                    tvSubmit.setVisibility(View.GONE);
                } else {
                    tvModifyPrescription.setVisibility(View.VISIBLE);
                }

                if (prescriptionStatusDto.data.status == 2) {
                    if (prescriptionStatusDto.data.level <= 5) {
                        tvSubmit.setVisibility(View.VISIBLE);
                    } else {
                        tvSubmit.setVisibility(View.GONE);
                    }
                }
            }

            @Override
            public void onError(Throwable e) {
                dismissProgressDialog();
                ToastUtil.onError(mContext, e);
            }

            @Override
            public void onComplete() {
                dismissProgressDialog();
            }
        });
    }

    public void submitPrescriptionSecond() {
        RestProxy.getInstance().submitPrescriptionSecond(prescriptionId, new Observer<BaseStringResponse>() {
            @Override
            public void onSubscribe(Disposable d) {

            }

            @Override
            public void onNext(BaseStringResponse baseStringResponse) {
                finish();
                EventBus.getDefault().post(new PrescriptionAchieveEvent());
            }

            @Override
            public void onError(Throwable e) {

            }

            @Override
            public void onComplete() {

            }
        });
    }

    @OnClick({R.id.tv_modify_prescription, R.id.tv_submit, R.id.tv_achieve})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tv_modify_prescription:
                finish();
                break;
            case R.id.tv_submit:
                submitPrescriptionSecond();
                break;
            case R.id.tv_achieve:
                EventBus.getDefault().postSticky(new PrescriptionAchieveEvent());
                finish();
                break;
            default:
                break;
        }
    }
}
